##*****************************************************************************
## TABLE A3: SUMMARY TABLE
##*****************************************************************************
## 
## R Version: R version 3.2.4 (2016-03-1)
##

##LOADING PACKAGES
library(texreg) ##Version: 1.36.4
library(xtable) ##Version: 1.8-2

##SET WORKING DIRECTORY
setwd('/workspace/fed_courts/scaling_paper/')

##LOADING DATASET
fjc <- read.csv('replication_materials/data/bonica_sen_fed_judges.csv')

## Race
fjc$black <- 0
fjc$hispanic <- 0
fjc$asian <- 0
fjc$white <- 0
fjc$black[grepl('african',fjc$race.or.ethnicity,ignore.case=T)] <- 1
fjc$hispanic[grepl('hispanic',fjc$race.or.ethnicity,ignore.case=T)] <- 1
fjc$asian[grepl('asian',fjc$race.or.ethnicity,ignore.case=T)] <- 1
fjc$white[grepl('white',fjc$race.or.ethnicity,ignore.case=T)] <- 1

########################################
##GENDER
########################################
fjc$female <- ifelse(fjc$gender == 'F',1,0)



##ff <- read.csv('/workspace/fed_courts/scaling_paper/data/fjc_with_cf_imputed_scores.csv')
fjc <- fjc[(fjc$enter.year >= 1981 & fjc$president.name %in% c('Barack Obama','George H.W. Bush','George W. Bush','William J. Clinton','Ronald Reagan')),]



get.sum <- function(fjc.in){
    n.cf <- sum(!is.na(fjc.in$dime.cfscore))
    n.ncf <- sum(is.na(fjc.in$dime.cfscore))

    pct.miss <- mean(is.na(fjc.in$dime.cfscore),na.rm=T)
    m.cf <- mean(fjc.in$dime.cfscore,na.rm=T)
    m.cfi <- mean(fjc.in$imputed.dime.cfscore[is.na(fjc.in$dime.cfscore)],na.rm=T)

    m.jcs <- mean(fjc.in$jcs.score.dw,na.rm=T)
    m.rep<- mean(fjc.in$dem.pres==0,na.rm=T)
    m.age <- mean(fjc.in$enter.year -fjc.in$birth.year,na.rm=T)
    m.female <- mean(fjc.in$gender=='F',na.rm=T)
    m.black <- mean(fjc.in$black,na.rm=T)
    m.hispanic <- mean(fjc.in$hispanic,na.rm=T)
    m.asian <- mean(fjc.in$asian,na.rm=T)
    m.wq <- mean(fjc.in$well.qual,na.rm=T)
    m.nconts <- mean(fjc.in$nconts,na.rm=T)
    m.nconts.gte.1 <- mean(fjc.in$nconts==1,na.rm=T)
    m.nconts.gte.2 <- mean(fjc.in$nconts %in% 2:9, na.rm=T)
    m.nconts.gte.5 <- mean(fjc.in$nconts>=5,na.rm=T)
    m.nconts.gte.10 <- mean(fjc.in$nconts>=10,na.rm=T)
    m.senscore <- mean(fjc.in$senscore.dw,na.rm=T)
    m.pubdef <- mean(fjc.in$pub.def)
    m.pros <- mean(fjc.in$prosecutor)
    m.judge <- mean(fjc.in$judge)


    o <- round(c(pct.miss=pct.miss,
                 m.scores=m.cf,
                 m.iscores=m.cfi,
                 m.jcs=m.jcs,
                 ## m.senscore=m.senscore,
                 ## m.rep = m.rep,
                 m.age=round(m.age,1),
                 ##m.wq=m.wq,
                 pct.female=m.female,
                 pct.black=m.black,
                 pct.hispanic=m.hispanic,
                 pct.asian=m.asian,
                 ## pct.pubdef=m.pubdef,
                 ## pct.prosecutor=m.pros,
                 ## pct.judge=m.judge,
                 ## nconts=m.nconts,
                 ## nconts=m.nconts.gte.5,
                 nconts.1=m.nconts.gte.1,
                 nconts.2.9=m.nconts.gte.2,
                 nconts.gte.10=m.nconts.gte.10
                 ),2)

    o <- c(num=nrow(fjc.in),o)
    return(o)
}




stab <- rbind(Obama=get.sum(fjc[fjc$president.name=='Barack Obama',]),
              GWBush=get.sum(fjc[fjc$president.name=='George W. Bush',]),
              Clinton=get.sum(fjc[fjc$president.name=='William J. Clinton',]),
              GHWBush=get.sum(fjc[fjc$president.name=='George H.W. Bush',]),
              Reagan=get.sum(fjc[fjc$president.name=='Ronald Reagan',]),
              Male=get.sum(fjc[fjc$gender=='M',]),
              Female=get.sum(fjc[fjc$gender=='F',]),
              White=get.sum(fjc[fjc$white==1,]),
              Black=get.sum(fjc[fjc$black==1,]),
              Hispanic=get.sum(fjc[fjc$hispanic==1,]),
              Asian=get.sum(fjc[fjc$asian==1,]),
              District.Court=get.sum(fjc[fjc$court.type=='USDC',]),
              CoA=get.sum(fjc[fjc$court.type=='USCA',]),
              All=get.sum(fjc))
xtable(stab)





oo <- model.matrix(~-1 + pres ,data=impdata[use,])
oo <- oo[,colSums(oo) !=0]
fjc <- cbind(impdata[use,!(colnames(impdata) %in% c('pres','court.type'))],oo)

a1 <- round(apply(as.matrix(fjc[!is.na(fjc$dime.cfscore),]),2,function(z) mean(as.numeric(as.character(z)),na.rm=T)),2)
a2 <- round(apply(as.matrix(fjc[is.na(fjc$dime.cfscore),]),2,function(z) mean(as.numeric(as.character(z)),na.rm=T)),2)
aa <- cbind(a1,a2)[-1,]
aa
colnames(aa) <- c('Observed','Missing')

b1 <- round(apply(as.matrix(fjc[!is.na(fjc$dime.cfscore),]),2,function(z) sum(as.numeric(as.character(z)),na.rm=T)),2)
b2 <- round(apply(as.matrix(fjc[is.na(fjc$dime.cfscore),]),2,function(z) sum(as.numeric(as.character(z)),na.rm=T)),2)
bb <- cbind(b1,b2)[-1,]
bb
colnames(bb) <- c('Observed','Missing')
